﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Dm.Internal;
using Microsoft.EntityFrameworkCore.Dm.Storage.Internal;
using Microsoft.EntityFrameworkCore.Storage;

namespace AcmeDameng.EntityFrameworkCore
{
    public class FixedDmExecutionStrategy : DmExecutionStrategy
    {
        private readonly ExecutionStrategyDependencies Dependencies;
        public FixedDmExecutionStrategy(ExecutionStrategyDependencies dependencies) : base(dependencies)
        {
            Dependencies = dependencies;
        }

        public override async Task<TResult> ExecuteAsync<TState, TResult>(TState state, Func<DbContext, TState, CancellationToken, Task<TResult>> operation, Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>> verifySucceeded, CancellationToken cancellationToken)
        {

            try
            {
                return await operation(Dependencies.CurrentContext.Context, state, cancellationToken);
            }
            catch (Exception ex) // when (ExecutionStrategy.CallOnWrappedException(ex, DmTransientExceptionDetector.ShouldRetryOn))
            {
                throw new InvalidOperationException(DmStrings.TransientExceptionDetected, ex);
            }
        }

    }
}
